<?php

class Admin_OrderController extends Zend_Controller_Action
{

    public function init()
    {
        $option = array(
        	'layout'=>'layout',
        	'layoutPath'=>APPLICATION_PATH .'/layouts/scripts/admin/'
        	);
        Zend_Layout::startMvc($option);
        $this->model_order = new Application_Model_Order();
        $this->model_order_detail = new Application_Model_Orderdetail();
    }

    public function indexAction()
    {
        $this->view->headTitle("Đơn hàng");
    }

    public function listAction()
    {
        try{
            $param = $this->_request->getParam('param');
            if($param){
                $select = $this->model_order->Search_Order($param['keyword'], $param['state']);
            }
            else
                $select = $this->model_order->select();
            $adapter = new Zend_Paginator_Adapter_DbSelect($select);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $current = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($current);
            $this->view->orders = $paginator;
            $this->_helper->layout()->disableLayout();
        }
        catch(Zend_Exception $ex){
        	echo $ex;
        }
    }

    public function detailAction()
    {
        try {
            $id = $this->_request->getParam('id');
            if($id){
                $details = $this->model_order_detail->Get_Order_Detail_with_OrderID($id);
                if($details){
                    $this->view->details = $details;
                }
            }
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }

    public function changestateAction()
    {
        try{
            $param = $this->_request->getParam("param");
            if($param){
                echo $param['id'];
                $row = $this->model_order->find($param['id'])->current();
                if($row){
                    $row->order_state = $param['state'];
                    $row->save();
                    echo 'success';
                }
            }
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }

    public function deleteAction()
    {
        try{
            $id = $this->_request->getParam('id');
            if($id){
                $row = $this->model_order->find($id)->current();
                if($row){
                    $order_detail = new Application_Model_Orderdetail();
                    $order_detail->delete("order_id = $id");
                    $row->delete();
                }
            }
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }


}









